package com.jinghua.treatment.controller;

import com.jinghua.treatment.common.GlobalConstant;
import com.jinghua.treatment.common.PageMsg;
import com.jinghua.treatment.common.controller.BaseController;
import com.jinghua.treatment.common.domain.JSONResult;
import com.jinghua.treatment.common.exception.BizException;
import com.jinghua.treatment.entity.SysDoctor;
import com.jinghua.treatment.entity.SysPatient;
import com.jinghua.treatment.entity.SysWechatAccount;
import com.jinghua.treatment.entity.vo.DoctorVO;
import com.jinghua.treatment.entity.vo.PatientVO;
import com.jinghua.treatment.service.SysPatientService;
import com.jinghua.treatment.service.SysWechatAccountService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * 患者模型控制
 * Created by 王兴光 on 2018/3/16.
 */
@Controller
@RequestMapping("/patient")
public class SysPatientController extends BaseController {
    @Resource
    private SysPatientService patientService;

    @Autowired
    private SysWechatAccountService sysWechatAccountService;
    /*@ApiOperation(value = "测试")
    @GetMapping("/getPatientAll")
    private List<SysPatient> findAll(){
        return patientService.selectAll();
    }*/

    @ApiOperation(value = "获取患者信息列表，跳转患者列表首页")
    @GetMapping(value = "/getPatientListByDoctorId")
    public String findPatientListByDoctorId(Model model){
        Integer doctorId = 0;
        if(super.session.getAttribute("currentDoctor")!=null) {
            SysDoctor sysDoctor = (SysDoctor) session.getAttribute("currentDoctor");
            doctorId = sysDoctor.getDoctorId();
        }else{
            // 医生对象为空
        }
        System.out.println("----进入患者信息列表----doctorId:"+doctorId);
        // 获取患者数量
        int patientCount = patientService.selectPatientCount(doctorId);
        model.addAttribute("patientCount",patientCount);
        System.out.println("患者数量："+patientCount);

        // 获取最新咨询患者（最近一周）
        List<SysPatient> patients = patientService.selectByWeek(doctorId);
        // 存储
        model.addAttribute("doctorId",doctorId);
        model.addAttribute("patients",patients);
        return "patient/patientList";
    }

    @ApiOperation(value = "获取首页患者信息列表")
    @RequestMapping(value = "/getIndexPatientListByDoctorId",method = RequestMethod.POST)
    @ResponseBody
    public JSONResult findIndexPatientList(Model model){
        Integer doctorId = 0;
        if(super.session.getAttribute("currentDoctor")!=null) {
            SysDoctor sysDoctor = (SysDoctor) session.getAttribute("currentDoctor");
            doctorId = sysDoctor.getDoctorId();
        }else{
            // 医生对象为空
            doctorId = 39;
        }
        System.out.println("----进入患者信息列表----doctorId:"+doctorId);

        // 获取最新咨询患者（时间降序，五个）
        List<SysPatient> patients = patientService.selectByWeek(doctorId);
        //System.out.println(patients.size());
        return new JSONResult(GlobalConstant.SUCCESS_CODE,"获取首页患者信息列表",patients);
    }

    @ApiOperation("患者列表，搜索患者信息")
    @RequestMapping(value = "/searchPatient")
    public String searchPatient(String patientName,Model model){
        Integer doctorId = 39;
        if(session.getAttribute("currentDoctor")!=null) {
            doctorId = ((SysDoctor) super.session.getAttribute("currentDoctor")).getDoctorId();
        }
        System.out.println("---患者列表---");
        //doctorId = (Integer)session.getAttribute("doctorId");
        System.out.println("doctorId:"+doctorId);
        List<SysPatient> patients = patientService.selectByName(doctorId,patientName);
        System.out.println("size:"+patients.size());
        model.addAttribute("patients",patients);
        model.addAttribute("title","患者列表");
        model.addAttribute("fristEn",new String[]{"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","#"});
        return "patient/search_patient";
    }


    @ApiOperation(value="患者查询测试，医生编号必须")
    @GetMapping(value="/getPatientByDoctorId",params = {"doctorId"})
    private List<SysPatient> findByDoctorId(Integer doctorId){return patientService.selectByDoctorId(doctorId);}

    @ApiOperation(value = "最新患者查询，最近一周添加的患者")
    @GetMapping(value="/getPatientByWeek",params = {"doctorId"})
    private List<SysPatient> findByWeek(Integer doctorId){return patientService.selectByWeek(doctorId);};

    @ApiOperation(value = "根据患者名称模糊查询患者信息列表，支持拼音")
    @GetMapping(value="/getPatientByName",params = {"doctorId","patientName"})
    private List<SysPatient> findByName(Integer doctorId,String patientName){return patientService.selectByName(doctorId,patientName);};

    @ApiOperation(value="添加患者")
    @GetMapping(value="/addPatient")
    private int addPatient(SysPatient patient){return patientService.insert(patient);}

    @ApiOperation(value="修改患者个人信息")
    @GetMapping(value="/updatePatient")
    private int updatePatient(SysPatient patient){return patientService.updateByPrimaryKey(patient);}

    @ApiOperation(value = "获取患者数量")
    @GetMapping(value = "/getPatientCount",params = {"doctorId"})
    private int findPatientCount(Integer doctorId){return patientService.selectPatientCount(doctorId);}

    @ApiOperation(value = "获取患者信息")
    @RequestMapping(value = "/getPatientById",method = {RequestMethod.GET,RequestMethod.POST})
    @ResponseBody
    private JSONResult findPatientById(Integer patientId){
        System.out.println("patientId"+patientId);
        if(patientId == null || patientId == 0) {
            System.out.println("患者编号为空");
            patientId = 24;
        }
        return new JSONResult(GlobalConstant.SUCCESS_CODE,"获取患者信息",patientService.selectById(patientId));
    }

    @ApiOperation(value = "获取患者信息,返回片段")
    @RequestMapping(value = "/getPatientForPage",method = {RequestMethod.GET,RequestMethod.POST})
    private String getPatientForPage(Integer patientId,Model model){
        if(StringUtils.isEmpty(patientId))
            throw new BizException("参数丢失");
        SysPatient sysPatient = patientService.selectById(patientId);
        model.addAttribute("patient",sysPatient);
        return "patient/inquiry_patient_basicinfo";
    }



    @ApiOperation(value = "患者首次注册 - by kriss")
    @RequestMapping(value = "/patientRegist", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public JSONResult patientRegist(String phone, String smsCode, String patientName, String wid) {
        String regSMS = (String) session.getAttribute("regSMS");
        JSONResult result = null;
        if (regSMS != null && regSMS.equals(smsCode)) {
            SysPatient patient = patientService.selectByPhone(phone);
            if (patient != null) {
                result = jsonResult(GlobalConstant.FAIL_CODE, "该号码已经注册", null);
            } else {
                patient = patientService.patientRegiste(phone, patientName,wid);
                session.setAttribute("currentPatient",patient);
                // TODO 将来权限做完后 这里要修改
                result = jsonResult(GlobalConstant.SUCCESS_CODE, "ok", "/mod-page/patient_home.html");
            }
        } else {
            result = jsonResult(GlobalConstant.FAIL_CODE, "验证码错误", null);
        }
        return result;
    }

    @ApiOperation(value = "患者基本信息 - by kriss")
    @RequestMapping(value = "/patientInfo", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public JSONResult patientInfo(Integer patientId) {
       if(StringUtils.isEmpty(patientId))
            throw new BizException("参数丢失");
       SysPatient sysPatient =  patientService.selectById(patientId);
       return jsonResult(GlobalConstant.SUCCESS_CODE, "ok", sysPatient);
    }

    @ApiOperation(value = "跳转到基本修改页面 - by kriss")
    @RequestMapping(value = "/toPatientUpd", method = {RequestMethod.GET, RequestMethod.POST})
    public String toPatientUpd(Integer patientId,Model model) {
        if(StringUtils.isEmpty(patientId))
            throw new BizException("参数丢失");
        SysPatient sysPatient =  patientService.selectById(patientId);
        model.addAttribute("patient",sysPatient);
        return "patient/inquiry_patient_basicinfo_upd";
    }

    @ApiOperation(value = "更新基本信息页面 - by kriss")
    @RequestMapping(value = "/update", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public JSONResult update(SysPatient sysPatient) {
        patientService.update(sysPatient);
        return jsonResult(GlobalConstant.SUCCESS_CODE, "ok", sysPatient);
    }


    @ApiOperation(value = "加载患者的医生 - by kriss")
    @RequestMapping(value = "/loadMyDoctor", method = {RequestMethod.GET, RequestMethod.POST})
    public String loadMyDoctor(Integer patientId,Model model) {
        if(StringUtils.isEmpty(patientId))
            throw new BizException("参数丢失");
        List<DoctorVO> doctors = patientService.loadMyDoctor(patientId);
        model.addAttribute("doctors",doctors);
        return "/patient/my_doctors";
    }

    @ApiOperation(value="修改患者个人信息VO")
    @RequestMapping(value = "/updPatient", method = {RequestMethod.GET, RequestMethod.POST})
    private String updPatient(PatientVO patient,String smsCode ,Model model){
        String regSMS = (String) session.getAttribute("regSMS");
        if (regSMS != null && regSMS.equals(smsCode)) {
            SysPatient sysPatient = patientService.selectByPhone(patient.getPhone());
            if (sysPatient != null) {
                model.addAttribute("msgVO",new PageMsg("该号码已经注册","返回重填","javascript:history.back()"));
            } else {
                patientService.updateByVO(patient);
                model.addAttribute("msgVO",new PageMsg("操作成功","返回我的主页","/mod-page/patient/patient_center.html?patientId="+patient.getPatientId()));
            }
        } else {
                jsonResult(GlobalConstant.FAIL_CODE, "验证码错误", null);
                model.addAttribute("msgVO",new PageMsg("验证码错误","返回重填","javascript:history.back()"));
        }
       //model.addAttribute("msgVO",new PageMsg("操作成功","返回我的主页","/mod-page/patient/patient_center.html?patientId="+patient.getPatientId()));
        return  "common/msg";
    }

}
